package io.linfeng.modules.app.controller;

import io.linfeng.common.utils.R;
import io.linfeng.modules.admin.entity.AppUserEntity;
import io.linfeng.modules.admin.entity.SearchEntity;
import io.linfeng.modules.admin.service.SearchService;
import io.linfeng.modules.app.annotation.Login;
import io.linfeng.modules.app.annotation.LoginUser;
import io.linfeng.modules.app.utils.LocalUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linfeng
 * @date 2023/4/26 20:26
 */
@Api(tags = "移动端——用户历史搜索")
@RestController
@RequestMapping("app/search")
public class AppSearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private LocalUser localUser;


    @GetMapping("/getUserSearchHistory")
    @ApiOperation("获取用户搜索历史列表")
    public R getUserSearchHistory(){
        List<SearchEntity> list=new ArrayList<>();
        AppUserEntity user = localUser.getUser();
        if(user!=null){
           list = searchService.getSearchListByUid(user.getUid());
        }
        return R.ok().put("result",list);
    }


    @GetMapping("/getHotSearchHistory")
    @ApiOperation("获取热门搜索历史列表")
    public R getHotSearchHistory(){
        List<String> list=searchService.selectHotSearch();
        return R.ok().put("result",list);
    }

    @PostMapping("/deleteSearchById/{id}")
    @ApiOperation("根据searchId删除搜索历史")
    public R deleteSearchById(@PathVariable Long id){
        searchService.removeById(id);
        return R.ok();
    }


    @Login
    @PostMapping("/deleteSearchByUId")
    @ApiOperation("删除该用户全部搜索历史")
    public R deleteSearchByUId(@ApiIgnore @LoginUser AppUserEntity user){
        searchService.deleteSearchByUId(user.getUid());
        return R.ok();
    }

}
